package cn.code.mycloud.module.envir.controller.admin.statistics;

import cn.code.mycloud.framework.common.pojo.CommonResult;
import cn.code.mycloud.module.envir.controller.admin.statistics.vo.AqiLevelVO;
import cn.code.mycloud.module.envir.controller.admin.statistics.vo.ProvinceAqiVO;
import cn.code.mycloud.module.envir.dal.dataobject.aqi.AqiDO;
import cn.code.mycloud.module.envir.enums.DictTypeConstants;
import cn.code.mycloud.module.envir.service.aqi.AqiService;
import cn.code.mycloud.module.envir.service.grid.GridService;
import cn.code.mycloud.module.envir.service.usergrid.UserGridService;
import cn.code.mycloud.module.system.api.dict.DictDataApi;
import cn.code.mycloud.module.system.api.dict.dto.DictDataRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.*;
import java.util.stream.Collectors;

import static cn.code.mycloud.framework.common.pojo.CommonResult.success;

@Tag(name = "统计")
@RestController
@RequestMapping("/envir/statistics")
public class StatisticsController {
    @Resource
    private AqiService aqiService;

    @Resource
    private GridService gridService;

    @Resource
    private UserGridService userGridService;

    @Resource
//    private DictDataService dictDataService;
    private DictDataApi dictDataApi; // openFeign

//    省分组检查统计
    @GetMapping("/aqi/province/list")
    @Operation(summary = "省分组检查统计")
    @PermitAll
    public CommonResult<List<ProvinceAqiVO>> aqiProvinceList() {
//        按每个省分组
        List<ProvinceAqiVO> resultList = aqiService.selectAqiProvinceList();
        return success(resultList);
    }
//    AQI空气质量指数级别分布
    @GetMapping("/aqi/level/list")
    @Operation(summary = "AQI空气质量指数级别分布")
    @PermitAll
    public CommonResult<List<AqiLevelVO>> aqiLevelList() {
        List<AqiLevelVO> resultList = new ArrayList<>();

        List<AqiDO> aqiDOS = aqiService.selectAllDone();
        // 首先计算已存在的aqiLevel及其数量
        Map<String, Long> groupSizeByAqiLevel = aqiDOS.stream()
                .collect(Collectors.groupingBy(AqiDO::getAqiLevelConfirm, Collectors.counting()));

        // 空气质量等级列表，确保包含了所有可能的等级
        CommonResult<List<DictDataRespDTO>> dictDataListResult = dictDataApi.getDictDataList(DictTypeConstants.ENVIR_AQI_LEVEL);
        dictDataListResult.getData().forEach(dictDataRespDTO -> {
            AqiLevelVO aqiLevelVO = new AqiLevelVO();
            aqiLevelVO.setId(UUID.randomUUID().toString());
            aqiLevelVO.setAqiLevel(dictDataRespDTO.getLabel());
            aqiLevelVO.setAqiLevelDesc(dictDataRespDTO.getValue());
            aqiLevelVO.setAqi(groupSizeByAqiLevel.getOrDefault(dictDataRespDTO.getValue(),0L));
            resultList.add(aqiLevelVO);
        });
        return success(resultList);
    }

}
